<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<!-- 		<image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
			</image>
			<text class="title">节水控制小程序忘记密码</text> -->
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="registerForm.username" class="input" type="text" placeholder="请输入手机号" maxlength="30" />
			</view>
			<view class="input-item flex align-center" style="width: 100%;margin: 0px;">
				<view class="iconfont icon-code icon"></view>
				<input v-model="registerForm.smsCode" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
				<view class="wrap flex1" style="min-width:220rpx ;color: #0081ff;">
					<u-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-code>
					<u-button type="text" @tap="getCode">{{tips}}</u-button>
				</view>
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="registerForm.password" :password="showPassword" class="input" placeholder="请输入新的密码"
					maxlength="30" />
				<u-icon :name="[!showPassword ? 'eye-fill' : 'eye-off']" @click="changePassword" size="26"></u-icon>
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="registerForm.confirmPassword" :password="showPassword2" class="input"
					placeholder="请确认新的密码" maxlength="30" />
				<u-icon :name="[!showPassword2 ? 'eye-fill' : 'eye-off']" @click="changePassword2" size="26"></u-icon>
			</view>

			<view class="action-btn">
				<button @click="handleRegister()" class="register-btn cu-btn block bg-blue lg round">修改密码</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSmsCodeImg,
		forgotPwd
	} from '@/api/login'

	export default {
		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 60,
				codeUrl: "",
				captchaEnabled: true,
				globalConfig: getApp().globalData.config,
				registerForm: {
					username: "",
					password: "",
					confirmPassword: "",
					smsCode: "",
				},
				showPassword: true,
				showPassword2: true

			}
		},
		created() {
			// this.getCode()
		},
		methods: {
			changePassword: function() {
				this.showPassword = !this.showPassword;
			},
			changePassword2: function() {
				this.showPassword2 = !this.showPassword2;
			},
			// 用户登录
			handleUserLogin() {
				this.$tab.navigateTo(`/pages/login`)
			},
			// 获取图形验证码
			getSmsCode() {
				if (!this.registerForm.username) {
					uni.$u.toast('请输入手机号');
					return
				}
				const isPhone = uni.$u.test.mobile(this.registerForm.username);
				if (!isPhone) {
					uni.$u.toast('请输入正确的手机号');
					return
				}
				getSmsCodeImg(this.registerForm.username).then(res => {
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
					// 通知验证码组件内部开始倒计时
					this.$refs.uCode.start();
					console.log(res, '验证吗');
					// this.registerForm.code = res.uuid
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.getSmsCode();
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				// uni.$u.toast('倒计时结束');
			},
			start() {
				// uni.$u.toast('倒计时开始');
			},
			// 注册方法
			async handleRegister() {
				const isPhone = uni.$u.test.mobile(this.registerForm.username);
				if (!isPhone) {
					uni.$u.toast('请输入正确的手机号');
					return
				}
				if (this.registerForm.username === "") {
					uni.$u.toast('请输入您的账号');
				} else if (this.registerForm.smsCode === "") {
					uni.$u.toast('请输入验证码');
				} else if (this.registerForm.password === "") {
					uni.$u.toast('请输入您的密码');
				} else if (this.registerForm.confirmPassword === "") {
					uni.$u.toast('请确认您的新密码');
				} else if (this.registerForm.confirmPassword !== this.registerForm.password) {
					uni.$u.toast('两次输入的密码不一致');
				} else {
					this.$modal.loading("修改密码中，请耐心等待...")
					this.register()
				}
			},
			// 用户注册
			async register() {
				forgotPwd(this.registerForm).then(res => {
					this.$modal.closeLoading()
					uni.showModal({
						title: "系统提示",
						content: "恭喜你，您的账号 " + this.registerForm.username + " 修改密码成功！",
						success: function(res) {
							if (res.confirm) {
								uni.redirectTo({
									url: `/pages/login`
								});
							}
						}
					})
				}).catch((e) => {
					console.log(e, 'e');
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.register-btn {
				margin-top: 40px;
				height: 45px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}
</style>